package com.example.simple.framework.security.handle;

import com.alibaba.fastjson2.JSON;
import com.example.simple.common.core.AjaxResult;
import com.example.simple.common.util.ServletUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 自定义登录失败处理器
 *
 * @author Fan
 * @date 2024/11/27
 */
@Component
public class LoginFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(exception.getMessage())));
    }
}

